package com.hanxu.mallproduct.service.impl;

import com.hanxu.mallproduct.entity.SpuInfoEntity;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanxu.common.utils.PageUtils;
import com.hanxu.common.utils.Query;

import com.hanxu.mallproduct.dao.SkuInfoDao;
import com.hanxu.mallproduct.entity.SkuInfoEntity;
import com.hanxu.mallproduct.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            String tmpKey = URLDecoder.decode(key, StandardCharsets.UTF_8);
            wrapper.and((queryWrapper) -> {
                queryWrapper.eq("sku_id", tmpKey).or().like("sku_name", tmpKey);
            });
        }

        String brandId = (String) params.get("brandId");
        if (StringUtils.hasText(brandId)) {
            wrapper.and((w) -> {
                w.eq("brand_id", brandId);
            });
        }

        String catelogId = (String) params.get("catelogId");
        if (StringUtils.hasText(catelogId)) {
            wrapper.and((w) -> {
                w.eq("catalog_id", catelogId);
            });
        }

        String min = (String) params.get("min");
        if (StringUtils.hasText(min)) {
            wrapper.and((w) -> {
                w.ge("price", min);
            });
        }

        String max = (String) params.get("max");
        if (StringUtils.hasText(max)) {
            wrapper.and((w) -> {
                w.le("price", max);
            });
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}